﻿using System;

/*
 Write a program that converts a number in the range [0...999]
 to a text corresponding to its English pronunciation. 
    Examples:
	0  "Zero"
	273  "Two hundred seventy three"
	400  "Four hundred"
	501  "Five hundred and one"
	711  "Seven hundred and eleven"
 */

class NumberToText
{
    static void Main(string[] args)
    {
        // user input validation
        int userInput = -1;
        bool userEnteredANumber;
        bool numberInRange;
        do
        {
            Console.Clear();
            Console.Write("Enter a number between 0 and 999: ");
            userEnteredANumber = Int32.TryParse(Console.ReadLine(), out userInput);
            numberInRange = (userInput >= 0 && userInput < 1000);
        }
        while (!userEnteredANumber ^ !numberInRange);
        
        // number patterns
        string[] numbersTo19Patterns = {
                                "zero",  "one",   "two",  "three", "four",   "five",   "six",
                                "seven", "eight", "nine", "ten",   "eleven", "twelve", "thirteen",
                                "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
                               };

        string[] tensPatterns = {
                                "","","twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
                                };

        // number to text algorithm
        string numberAsText = "";

        int hundreds = userInput / 100;
        if (hundreds != 0)
        {
            numberAsText += numbersTo19Patterns[hundreds] + " hundred";
        }

        int tens = userInput % 100;

        if ((hundreds != 0) && (tens != 0))
        {
            numberAsText += " and ";
        }

        switch (tens)
        {
            case 0: 
                {
                    break;
                }
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19: 
                {
                    numberAsText += numbersTo19Patterns[tens];
                    break;
                }
            default:
                {
                    // for tens bigger than 19
                    if((tens % 10) == 0)
                        {
                            numberAsText += tensPatterns[tens / 10];
                        }
                        else
                        {
                            numberAsText += tensPatterns[tens / 10] + " " + numbersTo19Patterns[tens % 10];
                        }
                    break;
                }
        }


        Console.WriteLine("You entered the number: {0}", numberAsText);
    }
}